package com.alm.token.ui.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.GlideApp;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.ui.data.bean.CorwdAddData;
import com.alm.token.ui.data.bean.CorwdIndexData;
import com.alm.token.ui.data.bean.DrawIndexData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.CrowdFundView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.Constant;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;

public class CrowdfundingActivity extends BaseActivity implements CrowdFundView<CorwdAddData> {


    @BindView(R.id.rightItem)
    TextView rightItem;
    @BindView(R.id.img_bizhongicon)
    ImageView imgBizhongicon;
    @BindView(R.id.tv_bizhongtv)
    TextView tvBizhongtv;
    @BindView(R.id.tv_totalmoney)
    TextView tvTotalmoney;
    @BindView(R.id.img_selectbizhong)
    ImageView imgSelectbizhong;
    @BindView(R.id.ly_ooutmoneybizhongtop)
    RelativeLayout lyOoutmoneybizhongtop;
    @BindView(R.id.tv_selectmoney)
    TextView tvSelectmoney;
    @BindView(R.id.tv_bizhongshow)
    TextView tvBizhongshow;
    @BindView(R.id.tv_bizhongprice)
    TextView tvBizhongprice;
    @BindView(R.id.tv_mathmoney)
    TextView tvMathmoney;
    @BindView(R.id.tv_g12price)
    TextView tvG12price;
    @BindView(R.id.tv_g12number)
    TextView tvG12number;
    @BindView(R.id.tv_zhongchoucommit)
    TextView tvZhongchoucommit;
    private CorwdIndexData.DataBean corwdIndexDataBean;
    private CorwdIndexData.DataBean.CfdBean cfd;
    private double g12Price;
    private List<CorwdIndexData.DataBean.WalletsBean> wallets = new ArrayList<>();
    private List<CorwdIndexData.DataBean.GoodsBean> goodsBeans = new ArrayList<>();
    private CorwdIndexData.DataBean.WalletsBean walletsBean;
    private CorwdIndexData.DataBean.GoodsBean goodsBean;
    private int moneyType = 1, selectBizhong = 0, selectGoods = 0;
    private double moneyPrice = 0.00d, number = 0.00d;
    private DecimalFormat dec1;

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_crowdfunding;
    }

    @Override
    protected void initVariables() {
        corwdIndexDataBean = ((CorwdIndexData) getIntent().getSerializableExtra("CorwdIndexDataBean")).getData();
        if (corwdIndexDataBean != null) {
            g12Price = corwdIndexDataBean.getG12Price();
            cfd = corwdIndexDataBean.getCfd();
            wallets = corwdIndexDataBean.getWallets();
            goodsBeans = corwdIndexDataBean.getGoods();
        }
    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.title_hint_item2))
                .setRightText(getString(R.string.title_hint_item13), this);


        if (wallets.size() != 0 && cfd != null && goodsBeans.size() != 0) {
            selectBizhong = 0;
            selectGoods = 0;

            mathInitView();

            if (goodsBeans.size() > 1) {
                tvSelectmoney.setOnClickListener(this);
            }

            if (wallets.size() > 1) {
                lyOoutmoneybizhongtop.setOnClickListener(this);
            } else {
                imgSelectbizhong.setVisibility(View.GONE);
            }
            tvZhongchoucommit.setOnClickListener(this);
        }

//        tvG12price.setText("≈$" + g12Price + "USD/G12");
        tvG12price.setText("≈$" + g12Price);
    }

    private void mathInitView() {
        goodsBean = goodsBeans.get(selectGoods);
        walletsBean = wallets.get(selectBizhong);

        moneyType = walletsBean.getId();


        if (moneyType == 1) {
            moneyPrice = cfd.getEthPrice();
            number = goodsBean.getEthPrice();
        } else {
            moneyPrice = cfd.getUsdtPrice();
            number = goodsBean.getUsdtPrice();
        }

        GlideApp.with(CrowdfundingActivity.this).load(walletsBean.getLogo())
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//                        if (EmptyUtils.isNotEmpty(resource)) {
//                            CommonUtil. changeSVGColor(CrowdfundingActivity.this, imgBizhongicon, resource, R.color.black);
//                            return true;
//                        }
                        return false;
                    }
                })
                .error(R.drawable.icon_arctic).into(imgBizhongicon);
        tvBizhongtv.setText(walletsBean.getName());
        tvTotalmoney.setText(walletsBean.getMoney() + walletsBean.getName());
        tvBizhongshow.setText(walletsBean.getName());
        tvBizhongprice.setText(moneyPrice + "USD/" + walletsBean.getName());

        tvSelectmoney.setText(number + "");
//        tvMathmoney.setText("≈$" + number * moneyPrice + "USD/" + walletsBean.getName());
        tvMathmoney.setText("≈$" + number * moneyPrice);
//        tvG12number.setText(number * moneyPrice / g12Price + " G12");

        tvG12number.setText(CommonUtil.saveToPoint4WithNo(number * moneyPrice / g12Price));
    }

    @Override
    protected void loadData() {
//        corwdIndexDataBean = (CorwdIndexData.DataBean) getIntent().getSerializableExtra("CorwdIndexDataBean");

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ly_ooutmoneybizhongtop:
                showPopupMenu1(lyOoutmoneybizhongtop);
                break;
            case R.id.rightItem:
                Intent intent = new Intent(this, VipOrderListActivity.class);
                intent.putExtra("intentType", 3);
                startActivity(intent);
                break;
            case R.id.tv_selectmoney:
                showPopupMenu2(tvSelectmoney);
                break;
            case R.id.tv_zhongchoucommit:

                UserWalletData.DataBean.SysMsgBean sysMsgBean1 = new UserWalletData.DataBean.SysMsgBean();
                sysMsgBean1.setTitle(getString(R.string.crowdfund_hint_warn1));
                sysMsgBean1.setContent(getString(R.string.crowdfund_hint_warn2, tvG12number.getText().toString(), Constant.G12ContractName));
                DialogUtils.showInfor1(this, sysMsgBean1, (pwd) -> {
                    preenter.getCorwdAddData(moneyType + "", number + "");
                });

//                preenter.getCorwdAddData(moneyType + "", number + "");
                break;

        }
    }

    @Override
    public void getData(CorwdAddData corwdAddData) {
        showToast(corwdAddData.getMsg());
        if (corwdAddData.getStatus() == 0) {
            finish();
        }


//        if(corwdAddData.){
//
//        }


    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        }else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(this,PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            finish();
            showToSetPaypswDialog();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }


    public void showPopupMenu1(View view) {
        //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载menu布局
        Menu menu = popupMenu.getMenu();
        for (CorwdIndexData.DataBean.WalletsBean walletsBean : wallets) {
            menu.add(walletsBean.getName());
        }
        popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu, menu);


        //设置menu中的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                for (int i = 0; i < wallets.size(); i++) {
                    if (item.getTitle().toString().equalsIgnoreCase(wallets.get(i).getName())) {
                        if (moneyType == wallets.get(i).getId()) {
                            showToast(getString(R.string.crowdfund_hint_warn3));
                            break;
                        }
                        selectBizhong = i;
                        mathInitView();
                        break;
                    }
                }
                return true;
            }
        });
        popupMenu.show();
    }


    public void showPopupMenu2(View view) {
        //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载menu布局
        Menu menu = popupMenu.getMenu();
        for (CorwdIndexData.DataBean.GoodsBean goodsBean : goodsBeans) {
            if (moneyType == 1) {//eth
                menu.add(goodsBean.getEthPrice() + (goodsBean.getStatus() != 1 ? getString(R.string.crowdfund_hint_warn4) : ""));
            } else {
                menu.add(goodsBean.getUsdtPrice() + (goodsBean.getStatus() != 1 ? getString(R.string.crowdfund_hint_warn4) : ""));
            }

        }
        popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu, menu);


        //设置menu中的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                String str;
                for (int i = 0; i < goodsBeans.size(); i++) {
                    if (moneyType == 1) {//eth
                        str = goodsBeans.get(i).getEthPrice() + "";
                    } else {
                        str = goodsBean.getUsdtPrice() + "";
                    }
                    if (item.getTitle().toString().indexOf(str) != -1) {
                        if (goodsBeans.get(i).getStatus() != 1) {
                            showToast(getString(R.string.crowdfund_hint_warn5));
                            break;
                        }
                        selectGoods = i;
                        mathInitView();
                        break;
                    }
                }
                return true;
            }
        });
        popupMenu.show();
    }

}
